package com.thon.service.system.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thon.dao.system.RolesDao;
import com.thon.dao.util.Page;
import com.thon.entity.system.OnlineUsers;
import com.thon.entity.system.Roles;
import com.thon.service.system.OnlineUsersService;
import com.thon.service.system.RolesService;

@Service
@Transactional
public class OnlineUsersServiceImpl implements OnlineUsersService {

	@Autowired
	private SessionDAO sessionDAO;

	@Override
	public List<OnlineUsers> readList() {
		
		List<OnlineUsers> listOnlineUsers = new ArrayList<OnlineUsers>();
		Collection<Session> sessions = sessionDAO.getActiveSessions();
		Iterator<Session> it = sessions.iterator();
		
		while(it.hasNext()){
			OnlineUsers onlineUser = new OnlineUsers();
		Session session = (Session) it.next();
		String loginName = session.getAttribute("org.apache.shiro.subject.support.DefaultSubjectContext_PRINCIPALS_SESSION_KEY").toString();
		//onlineUser.setStartTimestamp(session.getStartTimestamp());
		onlineUser.setTimeOut(session.getTimeout());
		onlineUser.setLoginName(loginName);session.getLastAccessTime();
		onlineUser.setSessionId((String) session.getId());
		onlineUser.setName((String) session.getId());
		onlineUser.setLoginName(loginName);
		listOnlineUsers.add(onlineUser);
		}	
		
		return listOnlineUsers;
	}

	@Override
	public Page<OnlineUsers> readUsersPage(Integer pageNo, Integer pageSize) {
		
		Page<OnlineUsers> page = new Page<OnlineUsers>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		List<OnlineUsers> listOnlineUsers = new ArrayList<OnlineUsers>();
		Collection<Session> sessions = sessionDAO.getActiveSessions();
		Iterator<Session> it = sessions.iterator();		
		while(it.hasNext()){
			OnlineUsers onlineUser = new OnlineUsers();
		Session session = (Session) it.next();
	
		if(session.getAttribute("org.apache.shiro.subject.support.DefaultSubjectContext_PRINCIPALS_SESSION_KEY")!=null)
			onlineUser.setLoginName(session.getAttribute("org.apache.shiro.subject.support.DefaultSubjectContext_PRINCIPALS_SESSION_KEY").toString());
			
		onlineUser.setStartTimestamp(sdf.format(session.getStartTimestamp()));
		onlineUser.setTimeOut(session.getTimeout());
		
		onlineUser.setSessionId( session.getId().toString());		
		
		if(session.getAttribute("org.apache.shiro.subject.support.DefaultSubjectContext_AUTHENTICATED_SESSION_KEY")!=null)
		onlineUser.setAuthenticated(session.getAttribute("org.apache.shiro.subject.support.DefaultSubjectContext_AUTHENTICATED_SESSION_KEY").toString());
		onlineUser.setLastAccessTime(sdf.format(session.getLastAccessTime()));
		onlineUser.setHost(session.getHost());
		listOnlineUsers.add(onlineUser);
		}	
		page.AutoSetListToPage(listOnlineUsers);
		return page;
	}	

	@Override
	public String kickOutUser(String sessionId) {
		Session session = sessionDAO.readSession(sessionId);
		session.setAttribute("kickOut", "true");
		
		
		return "success";
	}

	


}